package com.technoorbit.vbreakup;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONfunctions {

public static JSONObject connect(String url)
	
	{
	HttpClient httpclient = new DefaultHttpClient();
	// Prepare a request object
	
	HttpGet httpget = new HttpGet(url);
	// Execute the request
	
	HttpResponse response;
	
	JSONObject json = new JSONObject();
	
	try {
	
	response = httpclient.execute(httpget);
	HttpEntity entity = response.getEntity();
	if (entity != null) {
	// A Simple JSON Response Read
	InputStream instream = entity.getContent();
	String result= convertStreamToString(instream);
	json=new JSONObject(result);
	instream.close();
	}
	} catch (ClientProtocolException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	} catch (JSONException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	}
	 
	return json;
	}

	/**
	
	*
	* @param is
	* @return String
	*/
	public static String convertStreamToString(InputStream is) {
	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	StringBuilder sb = new StringBuilder();
	String line = null;
	try {
	while ((line = reader.readLine()) != null) {
	sb.append(line + "\n");

}
	} catch (IOException e) {
	e.printStackTrace();
	} finally {
	try {
	is.close();
	} catch (IOException e) {
	e.printStackTrace();
	}
	}
	return sb.toString();
	}
	
		public static String executeHttpPost(String Url,HashMap<String,String> params) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(Url);
            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
           /*  for (Iterator i = keys.iterator(); i.hasNext();) {
              Integer key = (Integer) i.next();
              String value = (String) params.get(key);
             }*/
            
            for (String key : params.keySet()) {
                //System.out.println("key/value: " + key + "/"+map.get(key));
                postParameters.add(new BasicNameValuePair(key.toString(),params.get(key)));
            }
            
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            String result = sb.toString();
            return result;
            } finally {
            if (in != null) {
                try {
                    in.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
		
		 public static List<String> ParseJsonListData(String strUrl,String Type)
		   	{
		    	
		    		 	  try {
		    		 		 JSONObject obj = JSONfunctions.connect(strUrl);
		    		 		 JSONArray array = obj.getJSONArray(Type);
		    		         List<String> listContent=new ArrayList<String>();
		    		         int arrayLength=array.length();
		    		         for(int i = 0, count = arrayLength;i<count;i++)
		    		         {
		    		        	 if(array.getJSONObject(i)!=null){
		    		             JSONObject category = array.getJSONObject(i);
		    		             
		    		             if(category!=null){
		    		            	 listContent.add(category.getString("ID")+" : "+category.getString("Title"));
		    		             }
		    		           }
		    		         }
		    		         
		    		         listContent.removeAll(Collections.singletonList(null));
		    		         return listContent;
		    		   } catch (JSONException e) {
		    		         e.printStackTrace();
		    		         return null;
		    		   }
		   	}
		 
	
	      
		 public static ArrayList<CustomList> ParseJsonMapData(String strUrl,String Type)
		   	{
			  
		    		 	  try {
		    		 		 JSONObject obj = JSONfunctions.connect(strUrl);
		    		 		 JSONArray array = obj.getJSONArray(Type);
		    		         List<String> listContent=new ArrayList<String>();
		    		         ArrayList<CustomList> listArray=new ArrayList<CustomList>();
		    		         int arrayLength=array.length();
		    		         for(int i = 0, count = arrayLength;i<count;i++)
		    		         {
		    		        	 if(array.getJSONObject(i)!=null){
		    		             JSONObject category = array.getJSONObject(i);
		    		             
		    		             if(category!=null){
		    		            	 int ID=Integer.parseInt(category.getString("ID"));
		    		            	 listArray.add(new CustomList(ID,category.getString("Title"),category.getString("Description")));
		    		            	// listContent.add(category.getString("Title")+":"+);
		    		             }
		    		           }
		    		         }
		    		         
		    		         listContent.removeAll(Collections.singletonList(null));
		    		         return listArray;
		    		   } catch (JSONException e) {
		    		         e.printStackTrace();
		    		         return null;
		    		   }
		   	}
	
}
